<a href="^Illustration::c:s0p1">Fig. 18.1</a> The type and the macros defined in header <b>stdarg.h</b>.<br>
<a href="^Code::c:s0p0">Fig. 18.2</a> Using variable-length argument lists.<br>
<a href="^Code::c:s0p1">Fig. 18.3</a> Using command-line arguments.<br>
<a href="^Code::c:s0p2">Fig. 18.4</a> Using functions <b>exit</b> and <b>atexit</b>.<br>
<a href="^Illustration::c:s0p2">Fig. 18.5</a> The signals defined in header <b>signal.h</b>.<br>
<a href="^Code::c:s0p3">Fig. 18.6</a> Using signal handling.<br>
<a href="^Code::c:s0p4">Fig. 18.7</a> Using <b>goto</b>.<br>
<a href="^Code::c:s0p5">Fig. 18.8</a> Printing the value of a <b>union</b> in both member data types.<br>
<a href="^Code::c:s0p6">Fig. 18.9</a> Using an anonymous <b>union</b>.<br>
<br>
</page>
<page>
<font size=18>Fig<a href="~audio/Ch18/18fig001.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>ure 18.1 - The type and the macros defined in header <b>stdarg.h</b>.<img src="graphics/ch18/fig18001.gif" ></font><br>
</page>
<page>
<font size=18><a href="~audio/Ch18/18fig005.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 18.5 - The signals defined in header <b>signal.h</b>.<img src="graphics/ch18/fig18005.gif" ></font><br>
[Note: This material is included for the benefit of C++
programmers who will work with C legacy code. In
C++, programmers use function overloading to
accomplish much of what C programmers accomplish
with variable-length argument lists.] It is possible to
create functions that receive an unspecified number of
arguments. <a href="^Errors::c:s0p0"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>An ellipsis (<b>...</b>) in a function's prototype
indicates that the function receives a variable number of
arguments of any type. Note that the ellipsis must
always be placed at the end of the parameter list, and
there must be at least one named parameter. The macros <br>
</page>
<page>
and definitions of the <i>variable arguments header
<tt></tt><b>stdarg.h </b></i>(<a href="^Illustration::c:s0p1"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 18.1</a>) provide the capabilities necessary
to build functions with variable-length argument lists. <br>
<spacer width=16 height=1>The program of <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.2</a> demonstrates function
<b>average</b> that receives a variable number of arguments.
The first argument of <b>average</b> is always the number of
values to be averaged. <br>
<spacer width=16 height=1>Function <b>average</b> uses all the definitions and macros of
header <b>stdarg.h</b>. Object <b>ap</b>, of type <b>va_list</b>, is used by
macros <b>va_start</b>, <b>va_arg</b>, and <b>va_end</b> to process the
variable-length argument list of function <b>average</b>. The
function begins by invoking <b>va_start</b> to initialize object
<b>ap</b> for use in <b>va_arg</b> and <b>va_end</b>. The macro receives <br>
</page>
<page>
two arguments--object <b>ap</b> and the identifier of the
rightmost argument in the argument list before the
ellipsis--<b>i</b> in this case (<b>va_start</b> uses <b>i</b> here to
determine where the variable-length argument list
begins). Next, function <b>average</b> repeatedly adds the
arguments in the variable-length argument list to
variable <b>total</b>. The value to be added to <b>total</b> is
retrieved from the argument list by invoking macro
<b>va_arg</b>. Macro <b>va_arg</b> receives two arguments--object
<b>ap</b>, and the type of the value expected in the argument
list--<b>double</b> in this case. The macro returns the value
of the argument. Function <b>average</b> invokes macro
<b>va_end</b> with object <b>ap</b> as an argument to facilitate a <br>
</page>
<page>
normal return to <b>main</b> from <b>average</b>. Finally, the
average is calculated and returned to <b>main</b>. Note that
we used only double arguments for the variable-length
portion of the argument list. Actually, any data type or a
mixture of data types can be used as long as the proper
type is specified each time <b>va_arg</b> is used.<br>
</page>
<page>
<b>Drag the correct identifier to the box associated with
A macro that is invoked before accessing a variable-length argument list.<component type="drop" width=72 height=18 name="va_start"> <br>
A macro that facilitates normal return from a function with a variable length argument list.<component type="drop" width=72 height=18 name="va_end"> <br>
To access the arguments in a variable length argument list, a maco of this type must be created.<component type="drop" width=72 height=18 name="va_list"> <br>
A macro that expands to an expression of the value and type of the next argument.<component type="drop" width=72 height=18 name="va_arg"> <br>
<component type=button name=b label="Check Your Answer" width=125 height=24>
</page>
</section>
<section type=Body name=Default title="18.4 Using Command-Line Arguments">
<page>
<font size=18 bold>18.4 Using Command-Line Arguments</font><hr>
On many systems--DOS and UNIX in particular--it is
possible to pass arguments to <b>main</b> from a command
line by including parameters <b>int</b> <b>argc</b> and <b>char</b> <b>*argv[]</b>
in the parameter list of <b>main</b>. Parameter <b>argc</b> receives
the number of command-line arguments. Parameter
<b>argv</b> is an array of strings in which the actual
command-line arguments are stored. Common uses of
command-line arguments include printing the
arguments, passing options to a program, and passing
filenames to a program. <br>
</page>
<page>
The program of<a href="^Code::c:s0p1"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.3</a> copies a file into another file
one character at a time. The executable file for the
program is called <b>copy</b>. A typical command line for the
<b>copy</b> program on a UNIX system is<br>
<font size=2><br></font><font size=11><pre>
$ copy input output<p>
</pre></font>
This command line indicates that file <b>input</b> is to be
copied to file <b>output</b>. When the program executes, if
<b>argc</b> is not <b>3</b> (<b>copy</b> counts as one of the arguments), the
program prints an error message and terminates.
Otherwise, array <b>argv</b> contains the strings "<b>copy</b>",
"<b>input</b>" and "<b>output</b>". The second and third
arguments on the command line are used as file names
by the program. The files are opened by creating <br>
</page>
<page>
<b>ifstream</b> object <b>inFile</b> and <b>ofstream</b> object <b>outFile</b>. If
both files are opened successfully, characters are read
from file <b>input</b> with member function <b>get</b> and written
to file <b>output</b> with member function <b>put</b> until the end-
of-file indicator for file <b>input</b> is set. Then the program
terminates. The result is an exact copy of file <b>input</b>.
Note that not all computer systems support command-
line arguments as easily as UNIX and DOS. Macintosh
and VMS systems, for example, require special settings
for processing command-line arguments. See the
manuals for your system for more information on
command-line arguments.<br>
</page>
<page>
<b>Drag the correct identifier to the box associated with
The name of the argument to main that is an array of strings representing the command-line arguments.<component type="drop" width=40 height=18 name="argv"> <br>
The name of the argument that represents the number of command-line arguments passed to main.<component type="drop" width=40 height=18 name="argc"> <br>
<component type=button name=b label="Check Your Answer" width=125 height=24>
</page>
</section>
<section type=Body name=Default title="18.5 Notes on Compiling Multiple-Source-File Programs">
<page>
<font size=18 bold>18.5 Notes on Compiling Multiple-Source-
File Programs</font><hr>
As stated earlier in the text, it is possible to build
programs that consist of multiple source files (see
Chapter 6, "Classes and Data Abstraction"). There are
several considerations when creating programs in
multiple files. For example, the definition of a function
must be entirely contained in one file--it cannot span
two or more files. <br>
<spacer width=16 height=1>In Chapter 3, we introduced the concepts of storage
class and scope. We learned that variables declared
outside any function definition are of storage class <br>
</page>
<page>
<b>static</b> by default and are referred to as global variables.
Global variables are accessible to any function defined
in the same file after the variable is declared. Global
variables also are accessible to functions in other files,
however, the global variables must be declared in each
file in which they are used. For example, if we define
global integer variable flag in one file, and refer to it
in a second file, the second file must contain the
declaration <br>
<font size=2><br></font><font size=11><pre>
extern int flag;<p>
</pre></font>
prior to the variable's use in that file. In the preceding
declaration, the storage class specifier extern
indicates to the compiler that variable flag is defined <br>
</page>
<page>
either later in the same file or in a different file. The
compiler informs the linker that unresolved references
to variable <b>flag</b> appear in the file (the compiler does not
know where the <b>flag</b> is defined, so it lets the linker
attempt to find <b>flag</b>). If the linker cannot locate a
definition of <b>flag</b>, a linker error is reported, and no
executable file is produced. <a href="^Perform::c:s0p0"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>If a proper global definition
is located, the linker resolves the references by
indicating where <b>flag</b> is located. <br>
<spacer width=16 height=1><a href="^Engineer::c:s0p0"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Just as <b>extern</b> declarations can be used to declare global
variables to other program files, function prototypes
can extend the scope of a function beyond the file in
which it is defined (the <b>extern</b> specifier is not required <br>
</page>
<page>
in a function prototype). <a href="^Portable::c:s0p0"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>This is accomplished by
including the function prototype in each file in which
the function is invoked, and compiling the files together
(see Section 17.2). Function prototypes indicate to the
compiler that the specified function is defined either
later in the same file or in a different file. The compiler
does not attempt to resolve references to such a
function--that task is left to the linker. If the linker
cannot locate a function definition, an error is
generated. <br>
<spacer width=16 height=1>As an example of using function prototypes to extend
the scope of a function, consider any program
containing the preprocessor directive <b>#include</b> <br>
</page>
<page>
<b><string.h></b>. This directive includes in a file the function
prototypes for functions such as <b>strcmp</b> and <b>strcat</b>.
Other functions in the file can use <b>strcmp</b> and <b>strcat</b> to
accomplish their tasks. The <b>strcmp</b> and <b>strcat</b>
functions are defined for us separately. We do not need
to know where they are defined. <a href="^Engineer::c:s0p1"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>We are simply reusing
the code in our programs. The linker resolves our
references to these functions automatically. This
process enables us to use the functions in the standard
library.<br>
<spacer width=16 height=1>It is possible to restrict the scope of a global variable or
function to the file in which it is defined. The storage
class specifier <b>static</b>, when applied to a global variable <br>
</page>
<page>
or a function, prevents it from being used by any
function that is not defined in the same file. This is
referred to as <i>internal linkage</i>. Global variables and
functions that are not preceded by <b>static</b> in their
definitions have <i>external linkage</i>--they can be accessed
in other files if those files contain proper declarations
and/or function prototypes. <br>
<spacer width=16 height=1>The global variable declaration <br>
<font size=2><br></font><font size=11><pre>
static float pi = 3.14159;<p>
</pre></font>
creates variable <b>pi</b> of type <b>float</b>, initializes it to
<b>3.14159</b>, and indicates that <b>pi</b> is known only to
functions in the file in which it is defined. <br>
</page>
<page>
The <b>static</b> specifier is commonly used with utility
functions that are called only by functions in a
particular file. If a function is not required outside a
particular file, the principle of least privilege should be
enforced by using <b>static</b>. If a function is defined before
it is used in a file, <b>static</b> should be applied to the
function definition. Otherwise, <b>static</b> should be applied
to the function prototype.<br>
<spacer width=16 height=1>When building large programs in multiple source files,
compiling the program becomes tedious if small
changes are made to one file, and the entire program
must be recompiled. Many systems provide special
utilities that recompile only the modified program file. <br>
</page>
<page>
On UNIX systems the utility is called <b><i>make</i></b>. Utility
<b>make</b> reads a file called <b><i>makefile</i></b> that contains
instructions for compiling and linking the program.
Systems such as Borland C++ and Microsoft Visual
C++ for PCs provide make utilities and "projects". For
more information on <b>make</b> utilities, see the manual for
Unix utility make reads a file called a makefile that contains instructions for compiling and linking multiple source files. <br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. Global variables are accessible to other files, but must be declared with storage class specifier extern in the files that do not define the global variable.">
Global variables are accessible to other files in a programwithout the need for a storage class specifier. <br>
<component type=button name=b label="Check Your Answer" width=125 height=24>
</page>
</section>
<section type=Body name=Default title="18.6 Program Termination with Exit and Atexit">
<page>
<font size=18 bold>18.6 Program Termination with Exit and
Atexit</font><hr>
The general utilities library (<b>stdlib.h</b>) provides methods
of terminating program execution other than a
conventional return from function <b>main</b>. Function <b><i>exit</i></b>
forces a program to terminate as if it executed normally.
The function often is used to terminate a program when
an error is detected in the input, or if a file to be
processed by the program cannot be opened. Function
<b><i>atexit registers</i></b> a function in the program to be called
upon successful termination of the program--i.e., either <br>
</page>
<page>
when the program terminates by reaching the end of
<b>main</b>, or when <b>exit</b> is invoked. <br>
<spacer width=16 height=1>Function <b>atexit</b> takes a pointer to a function (i.e., the
function name) as an argument. Functions called at
program termination cannot have arguments, and
cannot return a value. Up to 32 functions may be
registered for execution at program termination. <br>
<spacer width=16 height=1>Function <b>exit</b> takes one argument. The argument is
normally the symbolic constant <b>EXIT_SUCCESS</b> or
the symbolic constant <b>EXIT_FAILURE</b>. If <b>exit</b> is
called with <b>EXIT_SUCCESS</b>, the implementation-
defined value for successful termination is returned to
the calling environment. If <b>exit</b> is called with <br>
</page>
<page>
<b>EXIT_FAILURE</b>, the implementation-defined value
for unsuccessful termination is returned. When function
<b>exit</b> is invoked, any functions previously registered
with <b>atexit</b> are invoked in the reverse order of their
registration, all streams associated with the program are
flushed and closed, and control returns to the host
environment. The program of <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.4</a> tests functions
<b>exit</b> and <b>atexit</b>. The program prompts the user to
determine whether the program should be terminated
with <b>exit</b> or by reaching the end of <b>main</b>. Note that
function <b>print</b> is executed at program termination in
each case.<br>
</page>
<page>
<b>Select the true statement(s). </b><br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. The functions cannot take any arguments.">
Functions that are registered to be called at termination by atexit take a string argument which is displayed. <br>
<i>termination orders from the operating system</i>, and
<i>floating-point exceptions</i> (division by zero or
multiplying large floating-point values). The <i>signal
handling library</i> provides function <b><i>signal</i></b> to <i>trap</i>
unexpected events. Function <b>signal</b> receives two
arguments--an integer signal number and a pointer to
the signal handling function. Signals can be generated <br>
</page>
<page>
by function <b><i>raise</i></b> which takes an integer signal number
as an argument. <a href="^Illustration::c:s0p2"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 18.5</a> summarizes the standard
signals defined in header file <b>signal.h.</b> The program of
<a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.6</a> demonstrates functions <b>signal</b> and <b>raise</b>.<br>
<spacer width=16 height=1><a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 18.6</a> traps an interactive signal (<b>SIGINT</b>) with
function <b>signal</b>. The program calls <b>signal</b> with <b>SIGINT</b>
and a pointer to function <b>signal_handler</b> (remember
that the name of a function is a pointer to the the
function). Now, when a signal of type <b>SIGINT</b> occurs,
function <b>signal_handler</b> is called, a message is printed,
and the user is given the option to continue normal
execution of the program. If the user wishes to continue
execution, the signal handler is reinitialized by calling <br>
</page>
<page>
<b>signal</b> again (some systems require the signal handler to
be reinitialized), and control returns to the point in the
program at which the signal was detected. In this
program, function <b>raise</b> is used to simulate an
interactive signal. A random number between <b>1</b> and <b>50</b>
is chosen. If the number is <b>25</b>, then <b>raise</b> is called to
generate the signal. Normally, interactive signals are
initiated outside the program. For example, typing
<b><ctrl> c</b> during program execution on a UNIX or DOS
system generates an interactive signal that terminates
program execution. Signal handling can be used to trap
the interactive signal and prevent the program from
terminating.<br>
</page>
<page>
<b>Drag the correct term to the box associated with the
A <b>union</b> (defined with keyword <b>union</b>) is a region of
memory that, over time, can contain objects of a variety
of types. However, at any moment, a <b>union</b> can contain
a maximum of one object because the members of a
<b>union</b> share the same storage space. <a href="^Errors::c:s0p1"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>It is the
programmer's responsibility to ensure that the data in a
<b>union</b> is <a href="^Portable::c:s0p1"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>referenced with a member name of the proper
data type.<br>
<spacer width=16 height=1>At different times during a program's execution, some
objects may not be relevant, but one other object is--so
a <b>union</b> <b><a href="^Perform::c:s0p2"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a></b>shares the space instead of wasting storage on <br>
</page>
<page>
objects that are not being used. The number of bytes
used to <a href="^Portable::c:s0p3"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>store a <b>union</b> must be at least enough to hold
the largest member. <br>
<spacer width=16 height=1> <a href="^Portable::c:s0p2"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>A <b>union</b> is declared in the same format as a <b>struct</b> or a
<b>class</b>. The <b>union</b> declaration<br>
<font size=2><br></font><font size=11><pre>
union Number {<p>
int x;<p>
float y;<p>
};<p>
</pre></font>
indicates that <b>Number</b> is a <b>union</b> type with members
precedes <b>main</b> in a program so the definition can be
used to declare variables in all the program's functions. <br>
</page>
<page>
The only valid built-in operations that can be performed
on a <b>union</b> are: assigning a <b>union</b> to another <b>union</b> of
the same type, taking the address (<b>&</b>) of a <b>union</b>, and
accessing <b>union</b> members using the structure member
operator (<b>.</b>) and the structure pointer operator (<b>-></b>).
<b>union</b>s may not be <a href="^Errors::c:s0p3"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>compared for the same reasons that
structures cannot be compared.<br>
<spacer width=16 height=1>A <b>union</b> is similar to a class in that it can have a
constructor to initialize any of its members. A <b>union</b>
that has no constructor can be initialized with another
<b>union</b> of the same <a href="^Errors::c:s0p2"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>type, with an expression of the type
of the first member of the <b>union</b>, or with an initializer
(enclosed in braces) of the type of the first member of <br>
</page>
<page>
the <b>union</b>. <b>union</b>s can have other member functions,
such as destructors, but a <tt><b>union</b></tt>'s member functions
cannot be declared <b>virtual</b>. The members of a <b>union</b>
are <b>public</b> by default. <br>
<spacer width=16 height=1>A <b>union</b> cannot be used as a base class in inheritance,
i.e., classes may not be derived from <b>union</b>s. <b>union</b>s
can have objects as members only if these objects do
not have a constructor, a destructor, or an overloaded
assignment operator. None of a <b>union</b>'s data members
can be declared <b>static</b>.<br>
<spacer width=16 height=1>The program in<a href="^Code::c:s0p5"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.8</a> uses the variable <b>value</b> of
type <b>union</b> <b>number</b> to display the value stored in the
<b>union</b> as both an <b>int</b> and a <b>float</b>. The program output is <br>
</page>
<page>
implementation dependent. The program output shows
that the internal representation of a <b>float</b> value can be
quite different from the representation of an <b>int</b>.<br>
<spacer width=16 height=1>An <i>anonymous <b>union</b></i> is a <b>union</b> without a type name,
that does not attempt to define objects or pointers
before its terminating semicolon. Such a <b>union</b> does not
create a type but does create an unnamed object. An
anonymous <b>union</b>'s members may be accessed directly
in the scope in which the anonymous <b>union</b> is declared
just as any other local variable--there is no need to use
the dot (<b>.</b>) or arrow (<b>-></b>) operators. <br>
<spacer width=16 height=1>Anonymous <b>union</b>s have some restrictions.
Anonymous <b>union</b>s can contain only data members. All <br>
</page>
<page>
members of an anonymous <b>union</b> must be <b>public</b>. And,
an anonymous <b>union</b> declared globally (i.e., at file
scope) must be explicitly declared <b>static</b>. <br>
<spacer width=16 height=1><a href="^Code::c:s0p6"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 18.9</a> illustrates the use of an anonymous <b>union</b>.<br>
</page>
<page>
<b>Select the true statement(s). </b><br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. The members are public by default.">
The members of a union are private by default. <br>
All union members share the same address space. <br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. union definitions can be placed anywhere a struct or class can be placed.">
Unlike structs or classes, union definitions must be placed in a header file. <br>
<indent width=8 delay>* On many systems--UNIX and DOS systems in particular--it is possible to redirect input to a program and
output from a program. Input is redirected from the
UNIX and DOS command lines using the redirect input
symbol (<b><</b>) or using a pipe (<b>|</b>). Output is redirected from
the UNIX and DOS command lines using the redirect
output symbol (<b>></b>) or the append output symbol (<b>>></b>).
The redirect output symbol simply stores the program
output in a file and the append output symbol appends
the output to the end of a file.</indent>
<indent width=8 delay>* The macros and definitions of the variable arguments </indent>
</page>
<page>
<indent width=8 delay>* header <b>stdarg.h</b> provide the capabilities necessary to
build functions with variable-length argument lists. </indent>
<indent width=8 delay>* An ellipsis (<b>...</b>) in a function prototype indicates that
the function receives a variable number of arguments.</indent>
<indent width=8 delay>* Type <b>va_list</b> is suitable for holding information
needed by macros <b>va_start</b>, <b>va_arg</b>, and <b>va_end</b>. To
access the arguments in a variable-length argument list,
an object of type <b>va_list</b> must be declared.</indent>
<indent width=8 delay>* Macro <b>va_start</b> is invoked before the arguments of a
variable-length argument list can be accessed. The
macro initializes the object declared with <b>va_list</b> for use
by the <b>va_arg</b> and <b>va_end</b> macros.</indent>
<indent width=8 delay>* Macro <b>va_arg</b> expands to an expression of the value </indent>
</page>
<page>
<indent width=8 delay>* and type of the next argument in the variable-length
argument list. Each invocation of <b>va_arg</b> modifies the
object declared with <b>va_list</b> so that the object points to
the next argument in the list.</indent>
<indent width=8 delay>* Macro <b>va_end</b> facilitates a normal return from a
function whose variable argument list was referred to
by the <b>va_start</b> macro.</indent>
<indent width=8 delay>* On many systems--DOS and UNIX in particular--it
is possible to pass command-line arguments to <b>main</b> by
including in <b>main</b>'s parameter list the parameters <b>int</b>
<b>argc</b> and <b>char *argv[]</b>. Parameter <b>argc</b> is the number
of command-line arguments. Parameter <b>argv</b> is an array
of strings containing the command-line arguments. </indent>
</page>
<page>
<indent width=8 delay>* The definition of a function must be entirely contained in one file--it cannot span two or more files.</indent>
<indent width=8 delay>* Global variables must be declared in each file in
which they are used. </indent>
<indent width=8 delay>* Function prototypes can extend the scope of a function beyond the file in which it is defined (the <b>extern</b>
specifier is not required in a function prototype). This is
accomplished by including the function prototype in
each file in which the function is invoked and compiling the files together.</indent>
<indent width=8 delay>* The storage class specifier <b>static</b>, when applied to a
global variable or a function, prevents it from being
used by any function that is not defined in the same file. </indent>
</page>
<page>
<indent width=8 delay>* This is referred to as internal linkage. Global variables
and functions that are not preceded by <b>static</b> in their
definitions have external linkage--they can be accessed
in other files if those files contain proper declarations
and/or function prototypes. </indent>
<indent width=8 delay>* The <b>static</b> specifier is commonly used with utility
functions that are called only by functions in a particular file. If a function is not required outside a particular
file, the principle of least privilege should be enforced
by using <b>static</b>. </indent>
<indent width=8 delay>* When building large programs in multiple source
files, compiling the program becomes tedious if small
changes are made to one file, and the entire program </indent>
</page>
<page>
<indent width=8 delay>* must be recompiled. Many systems provide special utilities that recompile only the modified program file. On
UNIX systems the utility is called <b>make</b>. Utility <b>make</b>
reads a file called <b>makefile</b> that contains instructions
for compiling and linking the program. </indent>
<indent width=8 delay>* Function <b>exit</b> forces a program to terminate as if it
executed normally.</indent>
<indent width=8 delay>* Function <b>atexit</b> registers a function in a program to
be called upon normal termination of the program--i.e.,
either when the program terminates by reaching the end
of <b>main</b> or when <b>exit</b> is invoked.</indent>
<indent width=8 delay>* Function <b>atexit</b> takes a pointer to a function (i.e., a
function name) as an argument. Functions called at pro</indent>
</page>
<page>
<indent width=8 delay>* gram termination cannot have arguments, and cannot
return a value. Up to 32 functions may be registered for
execution at program termination. </indent>
<indent width=8 delay>* Function <b>exit</b> takes one argument. The argument is
normally the symbolic constant <b>EXIT_SUCCESS</b> or
the symbolic constant <b>EXIT_FAILURE</b>. If <b>exit</b> is
called with <b>EXIT_SUCCESS</b>, the implementation-
defined value for successful termination is returned to
the calling environment. If <b>exit</b> is called with
<b>EXIT_FAILURE</b>, the implementation-defined value
for unsuccessful termination is returned. </indent>
<indent width=8 delay>* When function <b>exit</b> is invoked, any functions registered with <b>atexit</b> are invoked in the reverse order of </indent>
</page>
<page>
<indent width=8 delay>* their registration, all streams associated with the program are flushed and closed, and control returns to the
host environment. </indent>
<indent width=8 delay>* The <b>volatile</b> qualifier is used to prevent optimizations of a variable because it can be modified from outside the program's scope.</indent>
<indent width=8 delay>* C++ provides integer and floating-point suffixes for
specifying the types of integer and floating-point constants. The integer suffixes are: <b>u</b> or <b>U</b> for an <b>unsigned</b>
integer, <b>l</b> or <b>L</b> for a <b>long</b> integer, and <b>ul</b> or <b>UL</b> for an
<b>unsigned long</b> integer. If an integer constant is not suffixed, its type is determined by the first type capable of
storing a value of that size (first <b>int</b>, then <b>long int</b>, then </indent>
</page>
<page>
<indent width=8 delay>* <b>unsigned long int</b>). The floating-point suffixes are: <b>f</b> or
<b>F</b> for a <b>float</b>, and l<b> </b>or <b>L</b> for a <b>long double</b>. A floating-
point constant that is not suffixed is of type <b>double</b>.</indent>
<indent width=8 delay>* The signal handling library provides the capability to
trap unexpected events with function <b>signal</b>. Function
<b>signal</b> receives two arguments--an integer signal number and a pointer to the signal handling function. </indent>
<indent width=8 delay>* Signals can also be generated with function <b>raise</b> and
an integer argument.</indent>
<indent width=8 delay>* The general utilities library (<b>stdlib.h</b>) provides functions <b>calloc</b> and <b>realloc</b> for dynamic memory allocation. These functions can be used to create dynamic
arrays. </indent>
</page>
<page>
<indent width=8 delay>* Function <b>calloc</b> receives two arguments--the number of elements (<b>nmemb</b>) and the size of each element
(<b>size</b>)--and initializes the elements of the array to zero.
The function returns either a pointer to the allocated
memory, or a <b>NULL</b> pointer if the memory is not allocated. </indent>
<indent width=8 delay>* Function <b>realloc</b> changes the size of an object allocated by a previous call to <b>malloc</b>, <b>calloc</b>, or <b>realloc</b>.
The original object's contents are not modified provided that the amount of memory allocated is larger
than the amount allocated previously. </indent>
<indent width=8 delay>* Function <b>realloc</b> takes two arguments--a pointer to
the original object (<b>ptr</b>) and the new size of the object </indent>
</page>
<page>
<indent width=8 delay>* (<b>size</b>). If <b>ptr</b> is <b>NULL</b>, <b>realloc</b> works identically to
<b>malloc</b>. If <b>size</b> is <b>0</b> and the pointer received is not
<b>NULL</b>, the memory for the object is freed. Otherwise,
if <b>ptr</b> is not <b>NULL</b> and <b>size</b> is greater than zero, <b>realloc</b>
tries to allocate a new block of memory for the object.
If the new space cannot be allocated, the object pointed
to by <b>ptr</b> is unchanged. Function <b>realloc</b> returns either
a pointer to the reallocated memory, or a <b>NULL</b> pointer.</indent>
<indent width=8 delay>* The result of the <b>goto</b> statement is a change in the
program's flow of control. Program execution continues at the first statement after the label in the <b>goto</b> statement. </indent>
<indent width=8 delay>* A label is an identifier followed by a colon. A label </indent>
</page>
<page>
<indent width=8 delay>* must appear in the same function as the <b>goto</b> statement
that refers to it. </indent>
<indent width=8 delay>* A <b>union</b> is a derived data type whose members share
the same storage space. The members can be any type.
The storage reserved for a <b>union</b> is large enough to
store its largest member. In most cases, <b>union</b>s contain
two or more data types. Only one member, and thus one
data type, can be referenced at a time.</indent>
<indent width=8 delay>* A <b>union</b> is declared in the same format as a structure. </indent>
<indent width=8 delay>* A <b>union</b> can be initialized only with a value of the
type of its first member. </indent>
<indent width=8 delay>* C++ enables the programmer to provide linkage
specifications to inform the compiler that a function </indent>
</page>
<page>
<indent width=8 delay>* was compiled on a C compiler, and to prevent the name
of the function from being encoded by the C++ compiler. </indent>
<indent width=8 delay>* To inform the compiler that one or several functions
have been compiled in C, write the function prototypes
as follows:</indent>
<font size=2><br></font><font size=11><pre>
extern "C" <i>function prototype</i> // single function<p>
extern "C" // multiple functions<p>
{<p>
<i>function prototypes</i><p>
}<p>
</pre></font>
<indent width=8 delay>* These declarations inform the compiler that the specified functions are not compiled in C++, so name </indent>
</page>
<page>
<indent width=8 delay>* encoding should not be performed on the functions
listed in the linkage specification. These functions can
then be linked properly with the program.</indent>
<indent width=8 delay>* C++ environments normally include the standard C
libraries and do not require the programmer to use linkage specifications for those functions.</indent>
</page>
<page>
<br>
</page>
<page>
</page>
</section>
<section type=Popup name=Debug title="Testing">
<page>
This chapter does not contain any Testing and Debugging tips.